package com.demon.androidbasic.adapter

import android.app.Activity
import android.view.View
import android.view.ViewGroup
import androidx.viewpager.widget.PagerAdapter
import com.hjq.base.http.glide.GlideApp
import com.luck.picture.lib.photoview.PhotoView

/**
 * 图片翻页适配器
 */
class ImagePagerAdapter(private val mActivity: Activity, private val mData: Array<String>) :
    PagerAdapter(), View.OnClickListener {
    override fun getCount(): Int {
        return mData.size
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view === `object`
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val view = PhotoView(mActivity)
        view.setOnClickListener(this)
        GlideApp.with(container.context)
            .load(mData[position])
            .into(view)
        container.addView(view)
        return view
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(`object` as View)
    }

    override fun onClick(v: View) {
        // 单击图片退出当前的 Activity
        if (!mActivity.isFinishing) {
            mActivity.finish()
        }
    }
}